package com.yu.gulimall.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.common.dto.MemberPrice;
import com.yu.common.dto.SkusReduceInfoDto;
import com.yu.common.utils.PageUtils;
import com.yu.common.utils.Query;
import com.yu.gulimall.coupon.dao.MemberPriceDao;
import com.yu.gulimall.coupon.dao.SkuFullReductionDao;
import com.yu.gulimall.coupon.dao.SkuLadderDao;
import com.yu.gulimall.coupon.entity.MemberPriceEntity;
import com.yu.gulimall.coupon.entity.SkuFullReductionEntity;
import com.yu.gulimall.coupon.entity.SkuLadderEntity;
import com.yu.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuLadderDao skuLadderDao;

    @Resource
    private MemberPriceDao memberPriceDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存打折信息  表：sms_sku_full_reduction
     *
     * @param skusReduceInfoDto
     */
    @Override
    public void saveInfo(SkusReduceInfoDto skusReduceInfoDto) {

        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtil.copyProperties(skusReduceInfoDto, skuLadderEntity);
        Integer countStatus = skusReduceInfoDto.getCountStatus();
        skuLadderEntity.setAddOther(countStatus);
        skuLadderDao.insert(skuLadderEntity);

        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtil.copyProperties(skusReduceInfoDto, skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(countStatus);
        this.save(skuFullReductionEntity);

        Long skuId = skusReduceInfoDto.getSkuId();
        List<MemberPrice> memberPrice = skusReduceInfoDto.getMemberPrice();
        memberPrice.forEach(item->{
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuId);
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setAddOther(countStatus);
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceDao.insert(memberPriceEntity);
        });
    }

}
